package com.shouer.doc.dialog;

import android.content.Context;
import android.view.View;

import com.shouer.doc.R;

/**
 * Created by ZXL on 2016/12/5.
 */

public class CommonDialogUtil implements DialogDismissListener{

    private static CommonDialogUtil instance;
    private CommonDialog commonDilaog;

    public CommonDialogUtil() {
    }

    public static CommonDialogUtil getInstance() {
        if (instance == null) {
            synchronized (CommonDialogUtil.class) {
                if (instance == null) {
                    instance = new CommonDialogUtil();
                }
            }
        }
        return instance;
    }

    public void showMessage(Context context, View.OnClickListener clickListener, String message, String leftBtn, String rigthBtn) {
        if (null == commonDilaog || !commonDilaog.isShowing()) {
            commonDilaog = new CommonDialog(context, R.style.dialog_untran);
        }
        show(clickListener, message, leftBtn, rigthBtn, 0);
    }

    public void showMessage(Context context, View.OnClickListener clickListener, String message, String leftBtn, String rigthBtn, int btnTag) {
        if (null == commonDilaog || !commonDilaog.isShowing()) {
            commonDilaog = new CommonDialog(context, R.style.dialog_untran);
        }
        show(clickListener, message, leftBtn, rigthBtn, btnTag);
    }

    public void showMessage(Context context, View.OnClickListener clickListener, String message, String centerBtntxt) {
        if (null == commonDilaog || !commonDilaog.isShowing()) {
            commonDilaog = new CommonDialog(context, R.style.dialog_untran);
        }
        show(clickListener, message, centerBtntxt, 0);
    }

    public void showMessage(Context context, View.OnClickListener clickListener, String message, String centerBtntxt, int btnTag) {
        if (null == commonDilaog || !commonDilaog.isShowing()) {
            commonDilaog = new CommonDialog(context, R.style.dialog_untran);
        }
        show(clickListener, message, centerBtntxt, btnTag);
    }


    private void show(View.OnClickListener clickListener, String message, String leftBtn, String rightBtn, int btnTag) {
        commonDilaog.withBtnClickListener(clickListener);
        commonDilaog.withMessage(message);
        commonDilaog.withBtnsText(leftBtn, rightBtn);
        commonDilaog.withBtnTag(btnTag);
        commonDilaog.withDialogDismissListener(this);
        commonDilaog.show();
    }

    private void show(View.OnClickListener clickListener, String message, String centerBtn, int btnTag) {
        commonDilaog.withBtnClickListener(clickListener);
        commonDilaog.withMessage(message);
        commonDilaog.withCenterBtnTxt(centerBtn);
        commonDilaog.withDialogDismissListener(this);
        commonDilaog.withBtnTag(btnTag);
        commonDilaog.show();
    }


    public void dismiss() {
        if (null != commonDilaog && commonDilaog.isShowing()) {
            commonDilaog.dismiss();
        }

    }

    @Override
    public void onDialogDismiss() {
        commonDilaog = null;
    }
}
